package com.lzj.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzj.demo.dto.StudentQueryVo;
import com.lzj.demo.enums.GradeEnum;
import com.lzj.demo.enums.SexEnum;
import com.lzj.demo.jdbc.DBUtils;
import com.lzj.demo.dto.Student;
import com.lzj.demo.mapper.StudentMapper;
import com.lzj.demo.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cursor.Cursor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service("studentService")
public class StudentServiceImpl extends ServiceImpl<StudentMapper,Student> implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Override
    public Student getStudentInfo(StudentQueryVo vo) {
        return lambdaQuery().eq(Student::getId,vo.getStudentId()).one();
    }

    @Override
    public List<Student> getStudentInfos(StudentQueryVo vo) {
        return lambdaQuery().list();
    }

    @Override
    @Transactional
    public void scan(int limit) throws Exception {
        try (Cursor<Student> cursor = studentMapper.scan(limit)) {
            cursor.forEach(student -> { });
        }
    }

    @Override
    public List<Student> listAllStudent(){
        List<Student> list = new ArrayList<>();
        Statement statement = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            String selectSql = "select * from student;";
            conn = DBUtils.getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery(selectSql);
            while (rs.next()) {
                Long id = rs.getLong("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                Student student = Student.builder()
                        .id(id)
                        .name(name)
                        .age(age)
                        .build();
                list.add(student);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.close(rs,statement,conn);
        }
        return list;
    }

    @Override
    public Boolean addStudent(StudentQueryVo vo){
        Student student = Student.builder()
                .name(vo.getStudentName())
                .age(vo.getStudentAge())
                //通用枚举处理
                .sex(SexEnum.getEnumByCode(vo.getStudentSex()))
                .grade(GradeEnum.getEnumByCode(vo.getStudentGrade()))
                .build();
        return save(student);

       /* Statement statement = null;
        Connection conn = null;
        int count = 0;
        try {
            String insertSql = "insert into student(id,name,age)"+" values('"+ 3 + "','"+ vo.getStudentName()
                    + "','" + vo.getStudentAge() +"')";
            conn = DBUtils.getConnection();
            statement = conn.createStatement();
            count = statement.executeUpdate(insertSql);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.close(null,statement,conn);
        }
        return count > 0;*/
    }

    @Override
    public Boolean updateStudent(StudentQueryVo vo){
        Statement statement = null;
        Connection conn = null;
        int count = 0;
        try {
            String updateSql = "update student set age = 25 where id = 3";
            conn = DBUtils.getConnection();
            statement = conn.createStatement();
            count = statement.executeUpdate(updateSql);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.close(null, statement, conn);
        }
        return count > 0;
    }

    @Override
    public Boolean deleteStudent(StudentQueryVo vo){
        //删除接口自动填充功能失效
        return removeById(vo.getStudentId());
        /*Statement statement = null;
        Connection conn = null;
        int count = 0;
        try {
           String deleteSql = "delete from student where id = 3";
           conn = DBUtils.getConnection();
           statement = conn.createStatement();
           count = statement.executeUpdate(deleteSql);
       }catch (Exception e){
            e.printStackTrace();
       }finally {
            DBUtils.close(null,statement,conn);
        }
        return count > 0;*/
    }
}
